﻿Imports ATS.Business.Entities
Imports ATS.Data
Imports System.Transactions
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class LocationComponent

#Region "CRUD"
    Public Function CreateLocation(_location As Location) As Location
        Return CreateNewLocation(_location)
    End Function

    Public Function ReadLocation(_location As String) As Location
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.ReadLocation(_location)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function ReadAllLocations() As List(Of Location)
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.ReadAllLocations()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function UpdateLocationRecord(_location As Location) As Location
        Return UpdateLocationRecordItem(_location)
    End Function


    Public Sub DeleteLocation(_location As Location)
        Dim dac As New AssetDataAccess
        Try
            dac.DeleteLocation(_location)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try

    End Sub
#End Region

#Region "Support Procedures"

    Private Function UpdateLocationRecordItem(_location As Location) As Location

        Using ts As New TransactionScope(TransactionScopeOption.Required)
            Try
                Dim dac As New AssetDataAccess()
                dac.UpdateLocation(_location)
                ts.Complete()
            Catch ex As TransactionAbortedException
                Console.WriteLine("TransactionAbortedException Message: {0}", ex.Message)
                Throw ex
            Catch ex As ApplicationException
                Console.WriteLine("ApplicationException Message: {0}", ex.Message)
                Throw ex
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Throw ex
            End Try
        End Using
        Return _location
    End Function

    Private Function CreateNewLocation(_location As Location) As Location
        Dim dac As New AssetDataAccess()
        Try
            Return dac.CreateLocation(_location)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function
#End Region
End Class
